import 'package:enjoy_pro_max/pages/home/index.dart';
import 'package:enjoy_pro_max/pages/mine/index.dart';
import 'package:enjoy_pro_max/utils/eventbus.dart';
import 'package:enjoy_pro_max/utils/token.dart';
import 'package:flutter/material.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  @override
  void initState() {
    super.initState();
    initToken();
    eventBus.on<LogoutEvent>().listen((event) {
      Navigator.pushNamed(context, '/login');
    });
  }

  initToken() async {
    await TokenManager().init();
    print(TokenManager().getToken());
  }

  // 高亮下标
  int currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
          child: IndexedStack(
            index: currentIndex,
            children: [
              const HomePage(),
              MinePage(
                currentIndex: currentIndex,
              )
            ],
          ),
        ),
        bottomNavigationBar: BottomNavigationBar(
          onTap: (int index) {
            setState(() {
              currentIndex = index;
            });
          },
          currentIndex: currentIndex,
          selectedItemColor: const Color.fromARGB(255, 85, 145, 175),
          unselectedItemColor: Colors.grey,
          type: BottomNavigationBarType.fixed,
          // 导航栏的所有选项卡都可以同时显示在屏幕上 并且导航栏的高度是固定的，不会根据内容自适应变化
          items: [
            BottomNavigationBarItem(
                icon: Image.asset(
                  'assets/tabs/home_default.png',
                  width: 32,
                  height: 32,
                ),
                activeIcon: Image.asset(
                  'assets/tabs/home_active.png',
                  width: 32,
                  height: 32,
                ),
                label: "首页"),
            BottomNavigationBarItem(
                icon: Image.asset(
                  'assets/tabs/my_default.png',
                  width: 32,
                  height: 32,
                ),
                activeIcon: Image.asset(
                  'assets/tabs/my_active.png',
                  width: 32,
                  height: 32,
                ),
                label: "我的"),
          ],
        ));
  }
}
